package com.lagou.xml;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.junit.Test;

import java.util.List;

/**
 * @Author wsl
 * @Date 2021/8/26 18:34
 * @Project_Name mysql
 */
public class TestBookXPath {
    /**
     * 需求：有books.xml，请解析出图书信息，并打印
     */

    @Test
    public void testBooks() throws DocumentException {

        //1.创建解析对象
        SAXReader sr = new SAXReader();

        //2.获取文档对象
        Document document =
     sr.read("E:\\IdeaStudyProjects\\mysql\\jdbc_module2_5\\src\\com\\lagou\\xml\\books.xml");

        //3.解析出图书信息
        List<Node> books = document.selectNodes("/books/book//*");

        for (Node node : books) {
            System.out.println(node.getName()+" = "+node.getText());
        }
    }
}
